package com.hailiao.beidou.message.ui.page.setting

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.booleanResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.d10ng.compose.ui.AppColor
import com.d10ng.compose.ui.AppText
import com.d10ng.compose.ui.PageTransitions
import com.d10ng.compose.ui.base.CellGroup
import com.d10ng.compose.ui.navigation.NavBar
import com.hailiao.beidou.message.R
import com.hailiao.beidou.message.bd.constant.ProtoVersion
import com.hailiao.beidou.message.data.store.contant.CommunicationMode
import com.hailiao.beidou.message.ui.page.destinations.StatusScreenDestination
import com.hailiao.beidou.message.ui.page.setting.view.SettingCell
import com.hailiao.beidou.message.ui.page.setting.view.SettingCellText
import com.hailiao.beidou.message.ui.view.BDStatusBar
import com.hailiao.beidou.message.utils.back
import com.hailiao.beidou.message.utils.go
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.annotation.RootNavGraph

/**
 * 设置
 * @Author d10ng
 * @Date 2024/3/4 16:00
 */
@RootNavGraph
@Destination(style = PageTransitions::class)
@Composable
fun SettingScreen(
    model: SettingScreenViewModel = viewModel()
) {
    val proto by model.protoFlow.collectAsState()
    val commMode by model.commModeFlow.collectAsState()
    SettingScreenView(
        proto = proto,
        commMode = commMode,
        onClickStatus = {
            go(StatusScreenDestination.route)
        },
        onClickProto = model::onClickProto,
        onClickCommMode = model::onClickCommMode
    )
}

@Composable
private fun SettingScreenView(
    proto: ProtoVersion = ProtoVersion.BD2,
    commMode: CommunicationMode = CommunicationMode.BLE,
    onClickStatus: () -> Unit = {},
    onClickProto: () -> Unit = {},
    onClickCommMode: () -> Unit = {}
) {
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(AppColor.Neutral.bg)
            .navigationBarsPadding()
    ) {
        NavBar(
            title = "设置",
            titleAlignment = Alignment.CenterStart,
            onClickBack = { back() }
        )
        BDStatusBar()
        Box(
            modifier = Modifier
                .fillMaxSize()
                .weight(1f)
        ) {
            if (booleanResource(id = R.bool.is_show_company_info)) {
                Column {
                    Spacer(modifier = Modifier.weight(1f))
                    Text(
                        text = "广东海聊科技有限公司 版权所有",
                        style = AppText.Normal.Hint.default,
                        textAlign = TextAlign.Center,
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .padding(top = 16.dp)
                    )
                    Text(
                        text = "Copyright © 2015 .All Rights Reserved.",
                        style = AppText.Normal.Hint.default,
                        textAlign = TextAlign.Center,
                        modifier = Modifier
                            .fillMaxWidth()
                            .wrapContentHeight()
                            .padding(top = 8.dp, bottom = 16.dp)
                    )
                }
            }
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(rememberScrollState())
            ) {
                CellGroup(title = "设备信息", border = false) {
                    SettingCell(
                        iconId = R.drawable.setting_status,
                        label = "设备状态",
                        onClick = onClickStatus
                    )
                }
                CellGroup(title = "通讯设置", border = false) {
                    SettingCellText(
                        iconId = R.drawable.setting_proto,
                        label = "协议版本",
                        text = proto.text,
                        onClick = onClickProto
                    )
                    SettingCellText(
                        iconId = R.drawable.setting_comm_type,
                        label = "设备连接",
                        text = commMode.text,
                        onClick = onClickCommMode
                    )
                }
                Box(modifier = Modifier.height(100.dp))
            }
        }
    }
}

@Preview
@Composable
private fun SettingScreenPreview() {
    SettingScreenView()
}